
import { ModalProps } from "antd";
import { useEffect, useState } from "react";
import { useAxiosWebGet } from "./axios";
import { useSetState } from "ahooks";

export const useModalQrcode = (_title: string, init?: any, onCancel?: () => void) => {
    const { fetch } = useAxiosWebGet('imeepos/miniprogram/qrcode')
    const [qrcodeUrl, setQrcodeUrl] = useState<string>(``)
    const [open, setOpen] = useState<boolean>(false)
    const [title, setTitle] = useState<string>(_title)
    const [params, setParams] = useSetState(init)
    useEffect(() => {
        if (!params) return;
        fetch(params).then(res => {
            const { data, success } = res;
            if (success) {
                setQrcodeUrl(`data:image/png;base64,${data}`)
            }
        })
    }, [params])
    const props: ModalProps = {
        open: open,
        onCancel: () => {
            setOpen(false)
            onCancel && onCancel()
        },
        destroyOnClose: true,
        title: title,
        footer: null
    }

    return {
        modalProps: props,
        qrcode: qrcodeUrl,
        setTitle,
        setParams,
        setOpen: (open: boolean, record?: any) => {
            setOpen(open)
            setParams(record)
        }
    }
}
